package com._520it.wms.web.controller;

import com._520it.wms.domain.Product;
import com._520it.wms.query.ProductQueryObject;
import com._520it.wms.service.IBrandService;
import com._520it.wms.service.IProductService;
import com._520it.wms.util.JSONResult;
import com._520it.wms.util.RequiredPermission;
import com._520it.wms.util.UploadUtil;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by HASEE on 2017/11/14.
 */
@Controller
@RequestMapping("/product")
public class ProductContorller {
    @Autowired
    private IProductService productService;
    //关联品牌
    @Autowired
    private IBrandService brandService;

    @RequestMapping("/list")
    @RequiredPermission("货品列表")
    public String list(Model model, @ModelAttribute("qo") ProductQueryObject qo) {
        //int a = 1/0; 异常解析器
        model.addAttribute("brands",brandService.listAll());
        model.addAttribute("pageResult", productService.query(qo));
        return "/product/list";
    }
    @RequestMapping("/selectProduct")
    public String selectProduct(Model model, @ModelAttribute("qo") ProductQueryObject qo) {
        //int a = 1/0; 异常解析器
        model.addAttribute("brands",brandService.listAll());
        model.addAttribute("pageResult", productService.query(qo));
        return "/product/selectProduct";
    }

    @RequestMapping("/input")
    @RequiredPermission("货品编辑")
    public String input(Long id, Model model) {
            model.addAttribute("brands", brandService.listAll());
        if (id != null) {
            Product product = productService.get(id);
            model.addAttribute("product", product);
        }
        return "/product/input";
    }

    @RequestMapping("delete")
    @RequiredPermission("货品删除")
    @ResponseBody
    public JSONResult delete(Long id,String imagePath,HttpServletRequest request) {
        if (!StringUtils.isEmpty(imagePath)) {
            UploadUtil.deleteFile(request.getServletContext(),imagePath);
        }
        if (id!=null){
            productService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission("货品新增/更新")
    @ResponseBody
    public JSONResult saveOrUpdate(Product product, MultipartFile pic, HttpServletRequest request) {
        //删除原来的图片--原来的imagePagth 不为空 新的pic 不为空
        if (!StringUtils.isEmpty(product.getImagePath()) && pic != null) {
            UploadUtil.deleteFile(request.getServletContext(), product.getImagePath());
        }
        if (pic != null) {
            String imagePath = UploadUtil.upload(pic, request.getServletContext().getRealPath("/upload"));
            product.setImagePath(imagePath);
        }

        productService.saveOrUpdate(product);
        return new JSONResult();

    }
}